package com.lanchetech.bean.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类
 */
public class DateUtils {
    //log
    private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    private DateUtils(){}


    //Srting转date
    public static Date changeStringToDate(String dateStr) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return date;

    }

    //Srting转date精确到毫秒
    public static Date changeStringToMillisecondDate(String dateStr) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return date;

    }

    //date转String精确到毫秒
    public static String changeDateToMillisecondString(Date date) {
        String dateStr = "";
        try {
            dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dateStr;
    }

}